﻿@page "/Member"
@using HuiLianMedical.Share
@using HuiLianMedical.Share.Data
@using Microsoft.EntityFrameworkCore
@inject IDbContextFactory<MedicalContext> dbContextFactory

<Drawer Closable="true" Width="600" Visible="Visible"
        Title='"更改"' OnClose="@CloseTable"
        Mask="false">
    <Form Model="ChangeModel"
          RequiredMark="FormRequiredMark.Optional"
          ValidateMode="@FormValidateMode.Rules"
          OnFinish="OnFinish">
        <FormItem Class="item" Label="姓名" >
            <Input @bind-Value="@context.UserName"/>
        </FormItem>
        <FormItem Class="item" Label="邮箱">
            <Input @bind-Value="@context.Email">
        </FormItem>
        <FormItem Class="item" Label="手机号码">
            <Input @bind-Value="@context.Phone">
        </FormItem>
        <FormItem Class="item" Label="密码">
            <Input @bind-Value="@context.Password">
        </FormItem>
        <FormItem Class="item" WrapperColOffset="8" WrapperColSpan="16">
            <Button Type="@ButtonType.Primary" HtmlType="submit">提交</Button>
        </FormItem>
    </Form>
</Drawer>

<Table TItem="UserModel" DataSource="@Users" Responsive>
    <PropertyColumn Title="姓名" Property="c => c.UserName"/>
    <PropertyColumn Title="编号" Property="c => c.Id"/>
    <PropertyColumn Title="头像路径" Property="c => c.Avatar"/>
    <PropertyColumn Title="邮箱" Property="c => c.Email"/>
    <PropertyColumn Title="手机号码" Property="c => c.Phone"/>
    <PropertyColumn Title="身份" Property="c => c.Identity"/>
    <PropertyColumn Title="点数" Property="c => c.Points"/>
    <ActionColumn Title="操作">
        <Space>
            <SpaceItem>
                <Button Danger OnClick="@(() => Delete(context))">删除</Button>
            </SpaceItem>
            <SpaceItem>
                <div class="phone-left-display">
                    <Button Danger OnClick="@(() => OpenTable(context))">更改</Button>
                </div>
            </SpaceItem>
        </Space>
    </ActionColumn>
</Table>

@code{

    private bool Visible;
    
    private List<UserModel> Users = [];

    protected override async Task OnInitializedAsync()
    {
        await using var context = await dbContextFactory.CreateDbContextAsync();
        Users = await context.Users.ToListAsync();
    }

    private async Task Delete(UserModel context)
    {
        await using var db = await dbContextFactory.CreateDbContextAsync();
        db.Users.Remove(context);
        await db.SaveChangesAsync();
        Users.Remove(context);
    }
    
    private UserModel ChangeModel { get; set; } = new();

    private void CloseTable()
    {
        Visible = false;
        ChangeModel = new UserModel();
    }

    private void OpenTable(UserModel model)
    {
        Visible = true;
        ChangeModel = model;
    }

    private async Task OnFinish()
    {
        await using var context = await dbContextFactory.CreateDbContextAsync();
        var f = await context.Users.FirstOrDefaultAsync(x => x.Id == ChangeModel.Id);

        if (f == null) return;

        f.UserName = ChangeModel.UserName;
        f.Password = ChangeModel.Password;
        f.Email = ChangeModel.Email;
        f.Phone = ChangeModel.Phone;

        await context.SaveChangesAsync();
    }

}